;; (DiskOut bufnum channels)

;; Note: There are constaints on the size of the buffer, it must be
;; greater than or equal to twice the size of the audio bus.  There
;; must be the same number of channels at the buffer and the DiskOut
;; ugen.

(let ((bus-size 1024)
      (bufferexpt 15))
  (= 0 (bitwise-and (expt 2 bufferexpt) 
		    (- (arithmetic-shift bus-size 1) 1))))

(synthdef-send
 (synthdef
  "DiskOut-Help"
  ((bufnum 0))
  (let ((z (OpClip2.ar 
	    (RLPF.ar (LFPulse.ar (SinOsc.kr 0.2 0 10 21) 
				 '(0 0.1) 0.1) 100 0.1) 0.4)))
    (DiskOut.ar bufnum z)
    (Out.ar 0 z))))

(define y
  (let ((b (buffer-alloc 32768 2 #t)))
    (buffer-write b 
		    "/tmp/TEST_DISK_OUT.aiff" 
		    "aiff"
		    "float"
		    32768
		    0
		    0
		    #t)
    (let ((n (synth-new "DiskOut-Help" (list "bufnum" (buffer-id b)))))
      (synth-finalize-push! n (lambda () (buffer-close b) (buffer-free b #f)))
      n)))

(synth-free y)

(system "sndfile-info /tmp/TEST_DISK_OUT.aiff")

(unlink "/tmp/TEST_DISK_OUT.aiff")
